package cn.big.netty.learn.echo.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Created by LiJianXin on 2019/5/8 0008.
 */
public class EchoServer {


	public static void main(String[] args) throws Exception {
		//接收一个传入的连接
		EventLoopGroup group = new NioEventLoopGroup();
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(group)
					.channel(NioServerSocketChannel.class)
					.localAddress(8888)
					//ChannelInitializer用于配置新的channel
					.childHandler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast(new EchoServerHandler());
						}
					});
			ChannelFuture channelFuture = serverBootstrap.bind().sync();
			System.out.println(EchoServer.class.getName() + "started and listen on " + channelFuture.channel().localAddress());
			channelFuture.channel().closeFuture().sync();
		} finally {
			group.shutdownGracefully();
		}
	}
}
